package com.example.chenjun.myapplication.net.okhttp.request;

import android.support.annotation.NonNull;

import java.util.Map;
import java.util.Set;

import okhttp3.Request;
import okhttp3.RequestBody;

public class GetRequest extends OkHttpRequest {
    public GetRequest(Map<String, String> params, Map<String, String> headers, @NonNull String url, String tag, String method) {
        super(params, headers, url, tag, method);
    }

    @Override
    protected RequestBody buildRequestBody() {
        return null;
    }

    private String appendUrl(){
        url += "?";
        Set<String> keySet = params.keySet();
        StringBuilder sb = new StringBuilder(url);
        for (String key : keySet) {
            sb.append(key).append("=");
            String value = params.get(key);
            sb.append(value).append("&");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }

    @Override
    public Request buildRequest() {
        Request.Builder builder = new Request.Builder();
        builder.url(appendUrl());
        Set<String> keySet = headers.keySet();
        for (String key : keySet) {
            builder.addHeader(key, headers.get(key));
        }
        return builder.build();
    }
}
